package com.wondersgroup.insurance.dnuilearn.combination.scrollanima;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/6/20.
 */

public class MyLinearLayout extends LinearLayout {
    public MyLinearLayout(Context context) {
        this(context, null);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {

        MyLayoutParams layoutParams = (MyLayoutParams) params;
        if (layoutParams !=null && layoutParams.isDiscrollvable()){
            MyFrameLayout frameLayout = new MyFrameLayout(getContext());
            frameLayout.addView(child);
            frameLayout.setmDiscrollveAlpha(layoutParams.mDiscrollveAlpha);
            frameLayout.setmDiscrollveFromBgColor(layoutParams.mDiscrollveFromBgColor);
            frameLayout.setmDiscrollveToBgColor(layoutParams.mDiscrollveToBgColor);
            frameLayout.setmDiscrollveScaleX(layoutParams.mDiscrollveScaleX);
            frameLayout.setmDiscrollveScaleY(layoutParams.mDiscrollveScaleY);
            frameLayout.setmDisCrollveTranslation(layoutParams.mDisCrollveTranslation);
            super.addView(frameLayout,params);
        }else {
            super.addView(child, params);
        }

    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MyLayoutParams(getContext(),attrs);
    }

    class MyLayoutParams extends LinearLayout.LayoutParams{
        public int mDiscrollveFromBgColor;//背景颜色变化开始值
        public int mDiscrollveToBgColor;//背景颜色变化结束值
        public boolean mDiscrollveAlpha;//是否需要透明度动画
        public int mDisCrollveTranslation;//平移值
        public boolean mDiscrollveScaleX;//是否需要x轴方向缩放
        public boolean mDiscrollveScaleY;//是否需要y轴方向缩放
        public MyLayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray ta= getContext().obtainStyledAttributes(attrs, R.styleable.DiscrollView_LayoutParams);
            mDiscrollveAlpha = ta.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_alpha,false);
            mDiscrollveScaleX = ta.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_scaleX, false);
            mDiscrollveScaleY = ta.getBoolean(R.styleable.DiscrollView_LayoutParams_discrollve_scaleY, false);
            mDisCrollveTranslation = ta.getInt(R.styleable.DiscrollView_LayoutParams_discrollve_translation, -1);
            mDiscrollveFromBgColor = ta.getColor(R.styleable.DiscrollView_LayoutParams_discrollve_fromBgColor, -1);
            mDiscrollveToBgColor = ta.getColor(R.styleable.DiscrollView_LayoutParams_discrollve_toBgColor, -1);
            ta.recycle();
        }

        private boolean isDiscrollvable(){
            return mDiscrollveAlpha||
                    mDiscrollveScaleX||
                    mDiscrollveScaleY||
                    mDisCrollveTranslation!=-1||
                    (mDiscrollveFromBgColor!=-1&&
                            mDiscrollveToBgColor!=-1);
        }
    }
}
